package com.zhang.service;

import com.zhang.domain.Girl;
import com.zhang.enums.ResultEnum;
import com.zhang.exception.GirlException;
import com.zhang.reposlitory.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;
    @Transactional
    public void inserTwo() {
        Girl girlA = new Girl();
        girlA.setAge(18);
        girlA.setCupSize("A");
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(19);
        girlB.setCupSize("F");
        girlRepository.save(girlB);

    }

    public void getAge(Integer id) throws Exception{
        Girl girl = girlRepository.findOne(id);
        Integer age = girl.getAge();
        if (age < 10) {
            // 上小学
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        } else if (age >= 10 && age < 16) {
            //  上初中
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        } else {
            // 其他
        }
    }

    /*
    * 通过ip查找
    * */
    public Girl findOne(Integer id) throws Exception{
        return girlRepository.findOne(id);
    }
}
